﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using QLNH.MyDialogs;
using QLNH.QLNH_BUS_WS;
using System.Threading;

namespace QLNH.MyForms
{
    public partial class AdminMenuEdit : FormUC
    {
        #region Properties

        private bool _edit;

        #endregion

        #region Constructor

        public AdminMenuEdit(LayoutUC mainLayout)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_MENU_EDIT;
            _edit = false;
        }

        public AdminMenuEdit(LayoutUC mainLayout, params object[] values)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_MENU_EDIT;
            _edit = true;
            FillValueIntoControl(values);
        }

        #endregion

        #region Functions

        //lay danh sach catalog & don vi tinh
        private void GetAllCatalogAndUnit()
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            CatalogDTO[] catalogs = ws.GetAllCatalog();
            List<string> units = ws.GetAllUnit();

            this.cb_catalog.BeginInvoke((MethodInvoker)(() => this.cb_catalog.DataSource = catalogs));
            this.cb_catalog.BeginInvoke((MethodInvoker)(() => this.cb_catalog.DisplayMember = "Name"));
            this.cb_catalog.BeginInvoke((MethodInvoker)(() => this.cb_catalog.ValueMember = "ID"));

            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            if (units.Count > 0)
                auto.AddRange(units.ToArray());

            this.tb_unit.BeginInvoke((MethodInvoker)(() => this.tb_unit.AutoCompleteCustomSource = auto));

            if (_edit)
            {
                this.cb_catalog.BeginInvoke((MethodInvoker)(() => this.cb_catalog.Text = _catalogName));
                this.bt_saveAndBack.BeginInvoke((MethodInvoker)(() => this.bt_saveAndBack.Enabled = false));
                this.bt_save.BeginInvoke((MethodInvoker)(() => this.bt_save.Enabled = false));
            }
        }

        //kiem tra va luu mon an moi
        private void CheckAndSave(bool goBack)
        {
            if (this.tb_name.Text.Trim().Equals(""))
            {
                this.lb_nameWarning.Visible = true;
                return;
            }

            if (this.tb_unit.Text.Trim().Equals(""))
            {
                this.lb_unitWarning.Visible = true;
                return;
            }

            if (this.tb_price.Text.Trim().Equals(""))
            {
                this.lb_priceWarning.Visible = true;
                return;
            }

            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();

            MealDTO meal = new MealDTO();
            meal.ID = this._mealID;
            meal.Name = this.tb_name.Text.Trim();
            meal.Unit = this.tb_unit.Text.Trim();
            meal.CurrentPrice = Double.Parse(this.tb_price.Text.Trim());
            meal.CatalogID = this.cb_catalog.SelectedValue.ToString();
            meal.Available = Boolean.Parse(this.cb_available.SelectedValue.ToString());
            if (_edit)
            {
                if (ws.EditMeal(meal) == true)
                {
                    MessageBox.Show("Cập nhật món ăn thành công");
                    if (goBack == true)
                        this._mainLayout.SwitchForm(FormKey.ADMIN_MENU_MANAGEMENT);
                    else
                    {
                        this.bt_save.Enabled = false;
                        this.bt_saveAndBack.Enabled = false;
                    }
                }
                else
                {
                    MessageBox.Show("Cập nhật món ăn thất bại");
                }
            }
            else
            {
                if (ws.AddMeal(meal) == true)
                {
                    MessageBox.Show("Thêm món mới thành công");
                    if (goBack == true)
                        this._mainLayout.SwitchForm(FormKey.ADMIN_MENU_MANAGEMENT);
                    else
                        ResetAllText();
                }
                else
                {
                    MessageBox.Show("Thêm món thất bại");
                }
            }
        End: ;
        }

        private string _mealID;
        private string _catalogName;
        private string _available;

        //dien du lieu vao trong cac textbox
        private void FillValueIntoControl(object[] values)
        {
            this._mealID = values[0].ToString();
            this.tb_name.Text = values[1].ToString();
            this.tb_unit.Text = values[2].ToString();
            this.tb_price.Text = values[3].ToString();
            this._catalogName = values[4].ToString();
            this._available = values[5].ToString();
        }

        //xoa du lieu trong cac textbox
        private void ResetAllText()
        {
            this.tb_name.Text = "";
            this.tb_unit.Text = "";
            this.tb_price.Text = "";

            this.tb_name.Focus();
            this.tb_name.SelectAll();
        }

        #endregion

        #region Control Events

        private void bt_addCatalog_Click(object sender, EventArgs e)
        {
            CatalogDLG addCatalogDLG = new CatalogDLG();
            if (addCatalogDLG.ShowDialog() == DialogResult.OK)
            {
                string catalogName = addCatalogDLG.tb_catalogName.Text.Trim();
                CatalogDTO catalog = new CatalogDTO();
                catalog.Name = catalogName;

                QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
                if (ws.AddCatalog(catalog) == true)
                {
                    MessageBox.Show("Thêm Catalog thành công");
                    Thread t = new Thread(new ThreadStart(GetAllCatalogAndUnit));
                    t.SetApartmentState(ApartmentState.STA);
                    t.Start();
                }
                else
                {
                    MessageBox.Show("Thêm Catalog thất bại");
                }
            }
        }

        private void InfoChanged(object sender, EventArgs e)
        {
            int tag = Int32.Parse(((Control)sender).Tag.ToString());
            switch (tag)
            {
                case 0:
                    if (this.lb_nameWarning.Visible == true)
                        this.lb_nameWarning.Visible = false;
                    break;
                case 1:
                    if (this.lb_unitWarning.Visible == true)
                        this.lb_unitWarning.Visible = false;
                    break;
                case 2:
                    if (this.lb_priceWarning.Visible == true)
                        this.lb_priceWarning.Visible = false;
                    break;
                default:
                    break;
            }

            if (_edit)
            {
                this.bt_save.Enabled = true;
                this.bt_saveAndBack.Enabled = true;
            }
        }

        private void tb_price_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            CheckAndSave(false);
        }

        private void bt_saveAndBack_Click(object sender, EventArgs e)
        {
            CheckAndSave(true);
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this._mainLayout.SwitchForm(FormKey.ADMIN_MENU_MANAGEMENT);
        }

        private void AdminMenuEdit_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(GetAllCatalogAndUnit));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();

            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Display", typeof(string)));
            table.Columns.Add(new DataColumn("Value", typeof(bool)));
            table.Rows.Add("Có sẵn", true);
            table.Rows.Add("Không có sẵn", false);

            this.cb_available.DataSource = table;
            this.cb_available.DisplayMember = "Display";
            this.cb_available.ValueMember = "Value";

            if (_edit)
            {
                this.cb_available.Text = _available;
            }
        }

        #endregion
    }
}
